﻿using System;
using Evpo.ContactData.Common;
using Evpo.ContactData.Data;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml.Linq;
using System.Data;

namespace Evpo.ContactData.ViewModel
{
    public class ExportViewModel
    {
        public ExportViewModel(IConnection conn)
        {
            _conn = (Connection)conn;
            _adapter = new ExportAdapter(_conn);
        }

        #region Fields
        private readonly Connection _conn;
        private readonly ExportAdapter _adapter;
        #endregion

        #region Properties
        #endregion

        #region Methods

        public IEnumerable<IExport> GetExports()
        {
            return _adapter.GetExportTable().Cast<IExport>();
        }

        public void GenerateExport(int id, Stream stm)
        {
            DataTable table = _adapter.FillDataTable(id);
            XmlDocumentGenerator generator = new XmlDocumentGenerator(table);
            generator.Generate(stm);
            stm.Position = 0L;
        }
        #endregion Methods

        #region DataSource

        public static IEnumerable<IExport> GetExports(ExportViewModel model)
        {
            return model.GetExports();
        }

        #endregion DataSource

    }
}
